package sections;

import static objects.AdapletConstants.sNewLine;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.LinkedHashMap;
import java.util.Map;

import objects.IObject;
import objects.Table;

public class TableSection implements ISection
{
	/**
	 * 
	 */
	private static final long serialVersionUID = -8411401262384797333L;
	Map<String, Table> fTableList;

	public TableSection()
	{
		fTableList = new LinkedHashMap<String, Table>();
	}

	public Table createNewTable(String tableName)
	{
		Table table = new Table();
		table.setTableName(tableName);

		fTableList.put(tableName, table);

		return table;
	}

	public Table getTable(String tableName)
	{
		Table table = fTableList.get(tableName);

		return table;
	}

	public void deleteTable(String tableName)
	{
		fTableList.remove(tableName);
	}

	private String buildHeaderComment()
	{
		StringBuffer commentBuf = new StringBuffer(500);

		commentBuf.append("#");
		commentBuf.append(sNewLine);
		commentBuf.append("################################################################################");
		commentBuf.append(sNewLine);
		commentBuf.append("#");
		commentBuf.append(sNewLine);
		commentBuf.append("# Definition of physical tables to foreign data source.  The following keywords");
		commentBuf.append(sNewLine);
		commentBuf.append("# are avalable:");
		commentBuf.append(sNewLine);
		commentBuf.append("#");
		commentBuf.append(sNewLine);
		commentBuf.append("#   column DATATYPE [primary]");
		commentBuf.append(sNewLine);
		commentBuf.append("#");
		commentBuf.append(sNewLine);
		commentBuf.append("#     where DATATYPE is the type of data contained in the column.");
		commentBuf.append(sNewLine);
		commentBuf.append("#       May be int, real, string, date, long, or raw");
		commentBuf.append(sNewLine);
		commentBuf.append("#");
		commentBuf.append(sNewLine);
		commentBuf.append("#   join table,table,table...");
		commentBuf.append(sNewLine);
		commentBuf.append("#     where \"condition\"");
		commentBuf.append(sNewLine);
		commentBuf.append("#");
		commentBuf.append(sNewLine);
		commentBuf.append("################################################################################");
		commentBuf.append(sNewLine);
		commentBuf.append("#");
		commentBuf.append(sNewLine);

		return commentBuf.toString();
	}

	public String buildTableSectionDefinition()
	{
		StringBuffer tableSectionDef = new StringBuffer(fTableList.size() * 1000);

		tableSectionDef.append(buildHeaderComment());
		tableSectionDef.append(sNewLine);

		for (String tableName : fTableList.keySet())
		{
			Table table = fTableList.get(tableName);

			// tableSectionDef.append("#Comments about the table?");
			tableSectionDef.append(table.toString());
			tableSectionDef.append(sNewLine);
		}

		return tableSectionDef.toString();
	}

	@Override
	public String toString()
	{
		return buildTableSectionDefinition();
	}

	@Override
	public int getCount()
	{
		return fTableList.size();
	}

	@Override
	public Section getSectionType()
	{
		return Section.TABLE;
	}

	@Override
	public IObject[] getObjects()
	{
		return fTableList.values().toArray(new Table[0]);
	}
	
	private void readObject(ObjectInputStream iStream) throws IOException, ClassNotFoundException
	{
		iStream.defaultReadObject();
		validateState();
	}
	
	private void validateState()
	{
		// TODO Auto-generated method stub
		
	}

	private void writeObject(ObjectOutputStream oStream) throws IOException
	{
		oStream.defaultWriteObject();
	}
}
